package com.cy.pj.common.web;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialException;
import java.time.LocalTime;

/*
*定义时间访问拦截器
* spring MVC模块中拦截器的标准为HandlerInterceptor接口
*/

public class TimeAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("======preHandle");
       LocalTime time=LocalTime.now();//LocalTime 为jdk8中的一个时间对象
        int hour=time.getHour();
        System.out.println("hour="+hour);
        if (hour>=9&&hour<=18)
            return true;
        throw new SerialException("不在规定的访问时间：9~18:00");
    }
}
